<?php
use Illuminate\Support\Facades\Redis;
function resp($code,$post = ''){
    $data = array('code'=>$code);
    if ($post && is_string($post)) $data['msg'] = $post;
    elseif ($post) $data = array_merge($data,$post);
    return $data;
}

function wxresp($code,$post = ''){
	$data = array('code'=>$code);
	if ($post && is_string($post)) $data['msg'] = $post;
	elseif ($post) $data = array_merge($data,$post);
	die(json_encode($data));
}

function redisTimeLimit($mid,$sec = 3, $msg='请勿频繁操作！'){
    if (Redis::get('redis_op_limit'.$mid) == 1){
        wxresp(-1, $msg);
    }
    Redis::setex('redis_op_limit'.$mid,$sec,1);
}

function strexists($string, $find) {
    return !(strpos($string, $find) === FALSE);
}

function timeFormat($score){
    if ($score == 0) return 0;
    $mscore = $score;
    $score = abs(intval($score));
    $str = '';
    if ($score > 3600){
        $hour = floor($score / 3600);
        $score -= $hour * 3600;
        if ($hour < 10) $str = "0";
        $str .= $hour.":";
    }
    $min = floor($score / 60);
    $sec = $score - $min * 60;
    if ($min < 10) $str .= "0";
    $str .= $min.":";
    if ($sec < 10) $str .= "0";
    $str .= $sec;
    if ($mscore < 0) return "-".$str;
    return $str;
}

function curPost($url,$post_data = '',$notjson = false,$headers = false) {
	$curl = curl_init();
	//设置抓取的url
	curl_setopt($curl, CURLOPT_URL, $url);
	//设置头文件的信息作为数据流输出
	curl_setopt($curl, CURLOPT_HEADER, 0);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
	//设置获取的信息以文件流的形式返回，而不是直接输出。
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	if ($headers) {
	    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
	}
	if ($post_data){
		//设置post方式提交
		curl_setopt($curl, CURLOPT_POST, 1);
		//设置post数据
		curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
	}
	//执行命令
	$data = curl_exec($curl);
	//关闭URL请求
	curl_close($curl);
	if ($notjson) return $data;
	return json_decode($data,true);
}

function multiPost($urls,$data = false,$headers = false) {
    $curls = $result = array();
    $handle = curl_multi_init();
    foreach($urls as $k => $v) {
        $curls[] = $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $v);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        if ($headers) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers[$k]);
        }
        if ($data){
            //设置post方式提交
            curl_setopt($curl, CURLOPT_POST, 1);
            //设置post数据
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data[$k]);
        }
        curl_multi_add_handle ($handle, $curl);
    }
    $active = null;
    do {
           $mrc = curl_multi_exec($handle, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    while ($active && $mrc == CURLM_OK) {
        if (curl_multi_select($handle) != -1) {
            do {
                $mrc = curl_multi_exec($handle, $active);
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
     }
    foreach ($curls as $v) {
        $result[] = (string) curl_multi_getcontent($v);
        curl_multi_remove_handle($handle, $v);
        curl_close($v);
    }
    curl_multi_close($handle);
    return $result;
}

function toBase64($data){
    return "data:image/jpeg;base64,".base64_encode($data);
}

function random($length, $numeric = FALSE) {
	$seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
	$seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
	if ($numeric) {
		$hash = '';
	} else {
		$hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
		$length--;
	}
	$max = strlen($seed) - 1;
	for ($i = 0; $i < $length; $i++) {
		$hash .= $seed[mt_rand(0, $max)];
	}
	return $hash;
}

function getip() {
	static $ip = '';
	$ip = $_SERVER['REMOTE_ADDR'];
	if(isset($_SERVER['HTTP_CDN_SRC_IP'])) {
		$ip = $_SERVER['HTTP_CDN_SRC_IP'];
	} elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
		$ip = $_SERVER['HTTP_CLIENT_IP'];
	} elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
		foreach ($matches[0] AS $xip) {
			if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
				$ip = $xip;
				break;
			}
		}
	}
	return $ip;
}

function uploadImage($img){
    if ($img){
        if (is_array($img)){
            foreach ($img as $k => $i) {
                if (strpos('A'.$i, 'data:image') > 0){
                    $image = explode(',',$i);
                    $image = $image[1];
                    $name = date('YmdHis').random(10,1).".png";
                    file_put_contents(public_path()."/uploads/".$name,base64_decode($image));
                    $img[$k] = $name;
                }elseif (strpos('A'.$i, "uploads/") > 0){
                    $img[$k] = str_replace("/uploads/", "", $i);
                }
            }
        }elseif (strpos('A'.$img, 'data:image') > 0){
            $image = explode(',',$img);
            $image = $image[1];
            $name = date('YmdHis').random(10,1).".png";
            file_put_contents(public_path()."/uploads/".$name,base64_decode($image));
            return $name;
        }elseif (strpos('A'.$img, "uploads/") > 0){
            return str_replace("/uploads/", "", $img);
        }
    }
    return $img;
}

function getImage($names,$fullpath = false){
    if (empty($names)) return $names;
    $host = '';
    if ($fullpath) $host = "https://".$_SERVER['HTTP_HOST'];
    if (is_array($names)){
        foreach ($names as $k => $value) {
            if (strpos('A'.$value, "http") > 0 || strpos('A'.$value, "//") > 0) continue;
            if (strpos('A'.$value, "/assets/") > 0) {
                if ($fullpath)  $names[$k] = $host.$value;
                else continue;
            }else $names[$k] = $host."/uploads/".$value;
        }
        return $names;
    }
    if (strpos('A'.$names, "http") > 0 || strpos('A'.$names, "//") > 0) return $names;
    if (strpos('A'.$names, "/assets/") > 0) {
        if ($fullpath) return $host.$names;
        else return $names;
    }
    return $host."/uploads/".$names;
}

function toUrlParams($post)
{
    $buff = "";
    foreach ($post as $k => $v)
    {
        if($k != "sign" && $v != "" && !is_array($v)){
            $buff .= $k . "=" . $v . "&";
        }
    }
    $buff = trim($buff, "&");
    return $buff;
}